package com.edu.atams.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.atams.common.dto.VotingStatistics;
import com.edu.atams.entity.Voting;
import com.edu.atams.entity.VotingClass;
import com.edu.atams.mapper.VotingMapper;
import com.edu.atams.service.VotingClassService;
import com.edu.atams.service.VotingOptionService;
import com.edu.atams.service.VotingPhotoService;
import com.edu.atams.service.VotingService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czj666
 * @since 2021-10-09
 */
@Service
public class VotingServiceImpl extends ServiceImpl<VotingMapper, Voting> implements VotingService {


    @Autowired
    VotingOptionService votingOptionService;

    @Autowired
    VotingPhotoService votingPhotoService;

    @Autowired
    VotingClassService votingClassService;

    @Autowired
    VotingMapper votingMapper;

    @Override
    public void saveInfo(Voting voting) {

        this.save(voting);

        voting.getVotingOptions().forEach(vo -> vo.setVotingId(voting.getId()));
        votingOptionService.saveBatch(voting.getVotingOptions());

        voting.getVotingPhotos().forEach(vp -> vp.setVotingId(voting.getId()));
        votingPhotoService.saveBatch(voting.getVotingPhotos());

        List<VotingClass> votingClasses = new ArrayList<>();
        voting.getClasses().forEach(c -> {
            VotingClass votingClass = new VotingClass();
            votingClass.setVotingId(voting.getId());
            votingClass.setClassId(c.getId());
            votingClasses.add(votingClass);
        });
        votingClassService.saveBatch(votingClasses);
    }

    @Override
    public Page<VotingStatistics> pageVotingDto(IPage page, Wrapper wrapper) {
        return votingMapper.findByCondition(page, wrapper);
    }

}
